package com.codeduck.properties.byFile;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Configurable;
import org.springframework.core.io.Resource;
import org.springframework.core.io.ResourceLoader;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;

/**
 * @auther Mahone Yan
 * @date 2023/5/5
 */
@Component
public class KafkaConsumerProperties {

    @Autowired
    private ResourceLoader resourceLoader;

    public Properties getConsumerProperties(String location) {
        InputStream inputStream = this.read(location);
        Properties props = new Properties();
        if (null != inputStream) {
            try {
                props.load(inputStream);
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }
        return props;
    }

    private InputStream read(String location) {
        InputStream inputStream = null;
        try {
            Resource resource = resourceLoader.getResource(location);
            inputStream = resource.getInputStream();
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
        return inputStream;
    }
}
